//
//  MaintenceTimeItemCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/20.
//

import UIKit

class MaintenceTimeItemCell: UICollectionViewCell {
    var bkgView: UIView!
    var titleLab: UILabel!
    var tipLab: UILabel!
    
    var tipBkgView: UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        bkgView = UIView()
        bkgView.backgroundColor = UIColor.white
        bkgView.layer.cornerRadius = 4
        bkgView.layer.masksToBounds = true
        contentView.addSubview(bkgView)
        
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = .clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        titleLab.numberOfLines = 1
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .center
        bkgView.addSubview(titleLab)
        
        tipBkgView = UIView()
        tipBkgView.layer.cornerRadius = 4
        tipBkgView.layer.masksToBounds = true
        tipBkgView.isHidden = true
        bkgView.addSubview(tipBkgView)
        
        tipLab = UILabel()
        tipLab.textColor = UIColor.init(hex: 0x939AA8)
        tipLab.backgroundColor = UIColor.init(hex: 0xAFBED7, alpha: 0.3)
        tipLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        tipLab.numberOfLines = 1
        tipLab.lineBreakMode = .byTruncatingTail
        tipLab.textAlignment = .center
        tipLab.text = MMLocalizedString("mainten_full", comment: "已满")
        tipBkgView.addSubview(tipLab)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLab.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        tipLab.sizeToFit()
        let tempW = tipLab.frame.size.width
        
        tipBkgView.snp.makeConstraints { make in
            make.left.equalTo(-4)
            make.top.equalTo(-4)
            make.width.equalTo(tempW + 16)
            make.height.equalTo(20)
        }
        
        tipLab.snp.makeConstraints { make in
            make.left.equalTo(4)
            make.top.equalTo(4)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setMaintenceTimeItemCell(data: TimeAvailabilityDataModel) {
        let start = data.startTime ?? ""
        self.titleLab.text = start.substring(to: 5)
        
        self.tipBkgView.isHidden = true
        self.bkgView.layer.borderWidth = 0
        self.bkgView.backgroundColor = UIColor.white
        self.titleLab.textColor = UIColor.init(hex: 0x21262C, alpha: 1)
        switch data.status {
            case 0:
                //不可选
                self.bkgView.backgroundColor = UIColor.init(hex: 0xAFBED7, alpha: 0.3)
                self.titleLab.textColor = UIColor.init(hex: 0x21262C, alpha: 0.6)
            case 2:
                //已满
                self.titleLab.textColor = UIColor.init(hex: 0x21262C, alpha: 0.6)
                self.tipBkgView.isHidden = false
                self.bkgView.backgroundColor = UIColor.init(hex: 0xAFBED7, alpha: 0.3)
            default:
                if data.selectFlag {
                    //选中状态
                    self.bkgView.backgroundColor = UIColor.init(hex: 0xFFCC32, alpha: 0.15)
                    self.bkgView.layer.borderWidth = 1
                    self.bkgView.layer.borderColor = UIColor.init(hex: 0xFFCC32).cgColor
                }
        }
        self.updateConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
